package com.top10.controller;

import com.top10.bean.ResponseBean;
import com.top10.entity.TbCategory;
import com.top10.service.IGoodsService;
import com.top10.service.impl.TbCategoryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *获取商品列表Controller
 */

@RestController
@RequestMapping("/category")
public class CategoryController {

	@Autowired
	private TbCategoryServiceImpl tbCategoryService;
	@Autowired
	private IGoodsService iGoodsService;

	@RequestMapping("/getparent")
	@ResponseBody
	public ResponseBean getParent()throws Exception{
		List<TbCategory> categoryList=tbCategoryService.getParent();
		return ResponseBean.createSuccess().addData("categoryList",categoryList);
	}
	@RequestMapping("/getson")
	@ResponseBody
	public ResponseBean getSon(Integer id)throws Exception{
		if(id == null){
			return ResponseBean.createError("输入的参数为空");
		}
		List<TbCategory> categoryList=tbCategoryService.getSon(id);
		if (categoryList.size()==0){
			return ResponseBean.createError("这类商品还没有下级商品");
		}else{
			return ResponseBean.createSuccess().addData("categoryList",categoryList);
		}

	}
	@RequestMapping("/getsonson")
	@ResponseBody
	public ResponseBean getSonSon(Integer id)throws Exception{
		if(id == null){
			return ResponseBean.createError("输入的参数为空");
		}
		List<TbCategory> categoryList=new ArrayList<>();
		List<TbCategory> categoryList1=tbCategoryService.getSon(id);
		if (categoryList1.size()==0){
			return ResponseBean.createError("这类商品还没有下级商品");
		}
		for(TbCategory tbCategory:categoryList1){
			List<String> show = iGoodsService.selectgoodsbycategorytwo(tbCategory.getUuid());
			if(show.size()!=0){
				tbCategory.setShow(show.get(0));
			}
			categoryList.add(tbCategory);
		}
		return ResponseBean.createSuccess().addData("categoryList",categoryList);
	}
}
